/* 
 * File:   UndoCommand.cpp
 * Author: Gab'z
 * 
 * Created on 3 juin 2012, 20:13
 */

#include "UndoCommand.h"
#include "Command.h"
#include "model/Context.h"

namespace calculator {

    UndoCommand::UndoCommand(const std::string name) : Command(name) {
    }

    UndoCommand::UndoCommand(const UndoCommand& orig) : Command(orig._name) {
    }

    UndoCommand::~UndoCommand() {
    }

    UndoCommand* UndoCommand::clone() const {
        return new UndoCommand(_name);
    }

    std::string UndoCommand::isExecutable() const {
        // executable s'il existe un memento
        if (!Context::getInstance()->getMementoCaretaker()->hasPrevious()) return "No previous memory";
        return "";
    }

    void UndoCommand::execute() const throw (CommandException) {
        // récupérer le memento
        if (!Context::getInstance()->getMementoCaretaker()->hasPrevious()) {
            throw CommandException("No previous memento");
        }
        // en récupérer la commande
        // demander à la commande de s'annuler
        Context::getInstance()->getMementoCaretaker()->getPreviousMemento()->getUndoableRedoableCommand()->undo();
    }

}
